package com.itheima.reggie.controller;

import com.itheima.reggie.common.BaseContext;
import com.itheima.reggie.common.R;
import com.itheima.reggie.entity.ShoppingCart;
import com.itheima.reggie.service.ShoppingCartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/shoppingCart")
public class ShoppingCartController {

    @Autowired
    private ShoppingCartService shoppingCartService;

    @GetMapping("/list")
    public R list() {
        List<ShoppingCart> list = shoppingCartService.getShoppingCartByUid(BaseContext.getCurrentId());
        return R.success(list);
    }


    @PostMapping("/add")
    public R add(@RequestBody ShoppingCart shoppingCart){
      ShoppingCart Cart =shoppingCartService.getAdd(shoppingCart);

        return R.success(Cart);
    }

    @DeleteMapping("/clean")
    public R clean() {
        shoppingCartService.cleanShoppingCat(BaseContext.getCurrentId());
        return R.success();
    }
@PostMapping("/sub")
    public R sub(@RequestBody ShoppingCart shoppingCart){
ShoppingCart cart =shoppingCartService.reduceToShoppingCat(shoppingCart);

        return R.success(cart);
    }


}

/*

1. 添加购物车
   判断一下购物车里是否已经包含指定的菜品/套餐
   1. 如果不包含: 直接往对应的表 ShoppingCart 添加一条数据. 数量1
   2. 如果已经包含: 执行update, 将之前的数量+1
2. 查看购物车 , 查询 ShoppingCart 数据,展示
3. 购物车中数量的变化
   减法最少到1, 再减则删除购物车的商品
4. 清空购物车, 根据用户ID删除

 */

